﻿using ImportPlcPointPosition.Models;
using Microsoft.IdentityModel.Tokens;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ImportPlcPointPosition.Forms
{
    public partial class TechniqueTemplateForm : BaseForm
    {
        public CksTechniqueTemplate Model = null;
        public TechniqueTemplateForm(CksTechniqueTemplate context = null)
        {
            InitializeComponent();
            if (context != null)
            {
                Model = context;
                txtName.Text = $"{Model.Name}";
                txtPlcId.Text = $"{Model.PlcId}";
                txtReadDB.Text = $"{Model.ReadDB}";
                txtWriteDB.Text = $"{Model.WriteDB}";
                txtSendAction.Text = $"{Model.SendActionId}";
                txtSyncAction.Text = $"{Model.SyncActionId}";
                txtSeq.Text = $"{Model.Sequence}";
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var item = Model ?? new CksTechniqueTemplate();
            item.Name = txtName.Text;
            item.PlcId = txtPlcId.Text.ObjToInt();
            item.Sequence = txtSeq.Text.IsNullOrEmpty() ? 99 : txtSeq.Text.ObjToInt();
            item.ReadDB = txtReadDB.Text.ObjToInt();
            item.WriteDB = txtWriteDB.Text.ObjToInt();
            item.SendActionId = txtSendAction.Text.ObjToInt();
            item.SyncActionId = txtSyncAction.Text.ObjToInt();
            item.Type = 1;
            if (item.Name.IsNullOrEmpty())
            {
                AntdUI.Message.error(this, "请填写必填项", autoClose: 2);
                return;
            }

            if (item.Id == 0)
                SqlSugarHelper.MesDB.Insertable(item).ExecuteCommand();
            else
                SqlSugarHelper.MesDB.Updateable(item).ExecuteCommand();
            DialogResult = DialogResult.OK;
        }
    }
}
